package com.xjtu.platform.reader.impl;

import com.github.pagehelper.PageInfo;
import com.xjtu.platform.common.PageParam;
import com.xjtu.platform.common.Utils;
import com.xjtu.platform.common.entity.FileEntity;
import com.xjtu.platform.common.page.QueryAction;
import com.xjtu.platform.common.page.QueryAction2;
import com.xjtu.platform.reader.ReserveService;
import com.xjtu.platform.reader.dao.ReaderDao;
import com.xjtu.platform.reader.dao.ReserveDao;
import com.xjtu.platform.reader.dto.BorrowHisDto;
import com.xjtu.platform.reader.dto.CancelReserveDto;
import com.xjtu.platform.reader.dto.ReserveBookDto;
import com.xjtu.platform.reader.dto.ReserveListDto;
import com.xjtu.platform.reader.entity.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ReserveServiceImpl implements ReserveService {

    @Resource
    private ReserveDao reserveDao;

    @Resource
    private ReaderDao readerDao;
    @Override
    public boolean reserveBook(String readerId, Timestamp nowTime, ReserveBookDto reserveBookDto) {
        try {
            reserveDao.reserveBook(reserveBookDto,readerId,nowTime);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map<String, Object> getReserveList(ReserveListDto reserveListDto) {
        return Utils.getPage2(reserveListDto, new QueryAction2<ReserveListEntity>() {
            @Override
            public PageInfo<ReserveListEntity> execute() {
                List<ReserveListEntity> list = reserveDao.getReserveHisList(reserveListDto);
                PageInfo<ReserveListEntity> page = new PageInfo<>(list);
                for( ReserveListEntity item : list){
                    BookBaseInfo bookBaseInfo = reserveDao.getReserveBookBaseInfo(item.getBook_id());
                    item.setBookBaseInfo(bookBaseInfo);

                    List<FileEntity> fileEntity = readerDao.getBookPic(item.getBook_id());
                    for(FileEntity s: fileEntity){
                        s.setFile_url(Utils.transAddToUrl(s.getFile_url()));
                    }
                    item.setFileList(fileEntity);


                    List<Author> authorList = readerDao.getBookAuthors(item.getBook_id());
                    item.setAuthorList(authorList);

                    List<BookLabel> bookLabelList = readerDao.getBookLabelList(item.getBook_id());
                    item.setLabelList(bookLabelList);

                    item.setBook_class(readerDao.getBookClass(item.getBook_id()));
                }
                PageInfo<ReserveListEntity> page2 = new PageInfo<>();
                BeanUtils.copyProperties(page,page2);
                page2.setList(list);
                return page2;
            }
        });
    }

    @Override
    public boolean cancelReserveById(CancelReserveDto cancelReserveDto) {
        try {
            reserveDao.cancelReserveById(cancelReserveDto);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map<String, Object> getPenaltyList(BorrowHisDto borrowHisDto) {
        return Utils.getPage2(borrowHisDto, new QueryAction2<UserPenaltyList>() {

            @Override
            public PageInfo<UserPenaltyList> execute() {
                List<UserPenaltyList> list = reserveDao.getPenaltyList(borrowHisDto);
                PageInfo<UserPenaltyList> basePage = new PageInfo<>(list);
                for(UserPenaltyList item : list){
                    item.setBook_name(reserveDao.getBookName(item.getBook_reader_his_id()));
                }
                PageInfo<UserPenaltyList> page2 = new PageInfo<>();
                BeanUtils.copyProperties(basePage,page2);
                page2.setList(list);
                return page2;
            }
        });
    }
}
